package com.jelly.io.system;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

/**
 * 标准IO重定向
 * @author Jelly
 *
 */
public class Redirecting {
	public static void main(String[] args){
		PrintStream consle = System.out;
		BufferedInputStream in=null;
		try {
			in = new BufferedInputStream(new FileInputStream("src\\com\\jelly\\io\\system\\Redirecting.java"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		PrintStream out=null;
		try {
			out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.setIn(in);
		System.setOut(out);
		System.setErr(consle);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s;
		try {
			while(null!=(s=br.readLine())){
				System.out.println(s);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.close();
		System.setOut(consle);
	}
}
